package org.nuist.repository;

import org.nuist.pojo.Relation;
import org.nuist.pojo.User;
import org.nuist.pojo.VirtualMachine;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface RelationRepository extends JpaRepository<Relation,Integer>{

    @Query(name = "findRelationById",nativeQuery = true,value = "select * from `web_shell`.relation where relation_id=:id")
    public Relation findRelationById(@Param("id") Integer id);

    @Query(name = "hasUser",nativeQuery = true,value = "select 1 from `web_shell`.relation where user_id=:id limit 1")
    public Integer hasUser(@Param("id") Integer id);

    @Query(name = "hasVM",nativeQuery = true,value = "select 1 from `web_shell`.relation where virtual_machine_id=:id limit 1")
    public Integer hasVM(@Param("id") Integer id);

    @Query(name = "findRelationByVMId",nativeQuery = true,value = "select * from `web_shell`.relation where relation.virtual_machine_id=:id")
    public Relation findRelationByVMId(@Param("id") Integer id);

    @Query(name = "findRelationByUserId",nativeQuery = true,value = "select * from `web_shell`.relation where relation.user_id=:id")
    public Relation findRelationByUserId(@Param("id") Integer id);


}
